package com.umbrella.worker.web.controller.manager;

import java.math.BigDecimal;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.umbrella.worker.dto.SupplierAccountDO;
import com.umbrella.worker.dto.SupplierDO;
import com.umbrella.worker.dto.SupplierPayrecordDO;
import com.umbrella.worker.query.AccountRecordQuery;
import com.umbrella.worker.query.SmsRecordQuery;
import com.umbrella.worker.query.SupplierAccountQuery;
import com.umbrella.worker.query.SupplierAccountRecordQuery;
import com.umbrella.worker.query.SupplierPayrecordQuery;
import com.umbrella.worker.query.SupplierQuery;
import com.umbrella.worker.result.JsonResultDO;
import com.umbrella.worker.result.JsonResultSupport;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.ISmsService;
import com.umbrella.worker.service.ISupplierAccountService;
import com.umbrella.worker.service.ISuppliersService;
import com.umbrella.worker.util.PageBeanUtil;

@Controller
@RequestMapping(value = "/supplier")
public class SuppliersContrlloer {

	@Autowired
	private ISuppliersService suppliersService;
	
	@Autowired
	private ISupplierAccountService supplierAccountService;
	
	@Autowired
	private ISmsService smsService;
	
	@RequestMapping(value = "/addSupplier.html", method = RequestMethod.GET)
	public ModelAndView join(ModelAndView mav, HttpServletRequest request) {
		mav.setViewName("manager/supplier/add");
		return mav;
	}
	
	@RequestMapping(value = "/join.json", method = RequestMethod.POST)
	public ModelAndView join(ModelAndView mav, 
			SupplierDO supplierDO,
			HttpServletRequest request) {
		
		JsonResultDO jsonResultDO = new JsonResultSupport();
		
		supplierDO.setCreateAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
		supplierDO.setModifiAuthor(supplierDO.getCreateAuthor());
		
		ResultDO result = suppliersService.create(supplierDO);
		
		if(result.isSuccess()) {
			jsonResultDO.setInfo("提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", "error");
		}
		return mav;
	} 
	
	/**
	 * 跳转到修改的页面
	 * @param mav
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/detail/{id}.html", method = RequestMethod.GET)
	public ModelAndView detail(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		//mav.setViewName("manager/supplier/join");
		
		ResultDO result = suppliersService.get(id);
		
		if(result.isSuccess()) {
			mav.addObject("supplier", result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.setViewName("manager/supplier/edit");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	
	/**
	 * 修改渠道商
	 * @param mav
	 * @param supplierDO
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/edit.html", method = RequestMethod.POST)
	public ModelAndView edit(ModelAndView mav, 
			SupplierDO supplierDO,
			HttpServletRequest request) {
		String author=(String) request.getSession().getAttribute("MANAGER_NAME");
		if(author==null || "".equals(author)){
			author="nothing";
		}
		supplierDO.setModifiAuthor(author);
		System.out.println(supplierDO.getwSAttacId()+" *************** ");
		supplierDO.setwSAttacId(supplierDO.getwSAttacId());
		
		ResultDO result = suppliersService.modifi(supplierDO);
		
		if(result.isSuccess()) {
			return new ModelAndView("redirect:/supplier/list/pass/1.html");
		} else {
			mav.setViewName("error");
		}
		return mav;
	} 
	
	@RequestMapping(value = "/remove/{id}.json", method = RequestMethod.GET)
	public ModelAndView remove(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		
		ResultDO result = suppliersService.remove(id);
		
		if(result.isSuccess()) {
			jsonResultDO.setInfo("删除提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("删除提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	} 
	
	@RequestMapping(value = "/pay/{id}.json", method = RequestMethod.GET)
	public ModelAndView paySuccess(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		//提现2为已经汇款
		ResultDO result = suppliersService.payStatus(id);
		
		if(result.isSuccess()) {
			
			jsonResultDO.setInfo("提现成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("提现失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	} 
	
	/**
	 * 批量删除
	 * @param mav
	 * @param idArray
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/removeall/{idArray}.json", method = RequestMethod.GET)
	public ModelAndView remove(ModelAndView mav, 
			@PathVariable(value="idArray") String idArray,
			HttpServletRequest request) {
		
		boolean isSuccess = true;
		JsonResultDO jsonResultDO = new JsonResultSupport();
		System.out.println(idArray);
		String[] ids = idArray.split("-");
		System.out.println(ids.length);
		ResultDO result = new ResultSupport();
		for(String id : ids) {
			System.out.println(id);
			result = suppliersService.remove(Integer.parseInt(id));
			if(!result.isSuccess()) {
				isSuccess = false;
				break;
			}
		}
		if(isSuccess) {
			jsonResultDO.setInfo("批量删除提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("批量删除提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	} 
	
	@RequestMapping(value = "/stop/{id}.json", method = RequestMethod.GET)
	public ModelAndView stop(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		ResultDO res =suppliersService.get(id);
		SupplierDO supplierDO = (SupplierDO) res.getModel(ResultSupport.FIRST_MODEL_KEY);
		//状态码 0：停用 1：启用  2:待审核（如果是管理员添加不用审核）
		supplierDO.setStatus(0);
		supplierDO.setModifiAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
		
		ResultDO result = suppliersService.modifi(supplierDO);
		
		if(result.isSuccess()) {
			jsonResultDO.setInfo("停用提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("停用提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	} 
	
	@RequestMapping(value = "/start/{id}.json", method = RequestMethod.GET)
	public ModelAndView start(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		ResultDO res =suppliersService.get(id);
		SupplierDO supplierDO = (SupplierDO) res.getModel(ResultSupport.FIRST_MODEL_KEY);
		//状态码 0：停用 1：启用  2:待审核（如果是管理员添加不用审核）
		supplierDO.setStatus(1);
		supplierDO.setModifiAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
		
		ResultDO result = suppliersService.modifi(supplierDO);
		
		if(result.isSuccess()) {
			jsonResultDO.setInfo("启用提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("启用提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	} 
	
	/**
	 * 审核 
	 * @param mav
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/check/{id}.json", method = RequestMethod.GET)
	public ModelAndView check(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		
		ResultDO res =suppliersService.get(id);
		SupplierDO supplierDO = (SupplierDO) res.getModel(ResultSupport.FIRST_MODEL_KEY);
		
		//状态码 0：停用 1：启用  2:待审核 3 不通过 4 通过
		String flag=request.getParameter("flag");
		if(flag!=null && !("".equals(flag))){
			supplierDO.setStatus(Integer.parseInt(flag));
		}
		
		supplierDO.setModifiAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
		
		ResultDO result = suppliersService.modifi(supplierDO);
		
		if(result.isSuccess()) {
			jsonResultDO.setInfo("审核提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("审核提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	} 
	
	/**
	 * 批量通过
	 * @param mav
	 * @param idArray
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/checkall/{idArray}.json", method = RequestMethod.GET)
	public ModelAndView checkall(ModelAndView mav, 
			@PathVariable(value="idArray") String idArray,
			HttpServletRequest request) {
		
		boolean isSuccess = true;
		JsonResultDO jsonResultDO = new JsonResultSupport();
		System.out.println(idArray);
		String[] ids = idArray.split("-");
		System.out.println(ids.length);
		ResultDO result = new ResultSupport();
		for(String id : ids) {
			System.out.println(id);
			ResultDO res =suppliersService.get(Integer.parseInt(id));
			SupplierDO supplierDO = (SupplierDO) res.getModel(ResultSupport.FIRST_MODEL_KEY);
			//状态码 0：停用 1：启用  2:待审核 3 不通过 4 通过
			supplierDO.setStatus(4);
			
			supplierDO.setModifiAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
			
			result = suppliersService.modifi(supplierDO);
			if(!result.isSuccess()) {
				isSuccess = false;
				break;
			}
		}
		if(isSuccess) {
			jsonResultDO.setInfo("批量通过提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("批量通过提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	} 
	
	@RequestMapping(value = "/list/{name}/{status}/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView query(ModelAndView mav,
			@PathVariable(value="name") String name,
			@PathVariable(value="status") Integer status,
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		
		SupplierQuery query = new SupplierQuery();
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		//启用 不启用  通过
		if(!name.equals("all")) {
			query.setName(name);
		}
		query.setStatus(status);
		
		ResultDO result = suppliersService.list(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			
			
			mav.setViewName("manager/supplier/list");

		} else {
			mav.setViewName("error");
		}
		
		mav.addObject("flag", status);
		return mav;
		
	}
	
	@RequestMapping(value = "/account.html", method = RequestMethod.GET)
	public ModelAndView account(ModelAndView mav, HttpServletRequest request) {
		Integer supplerId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		ResultDO result = suppliersService.getAccount(supplerId);
		if(result.isSuccess()) {
			mav.addObject("SUPPLIER_ACCOUNT", result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.setViewName("manager2/financial/fiAccount");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/exAndReInfo.html", method = RequestMethod.GET)
	public ModelAndView exAndReInfo(ModelAndView mav, HttpServletRequest request) {
		Integer supplerId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		ResultDO result = suppliersService.getAccount(supplerId);
		if(result.isSuccess()) {
			mav.addObject("SUPPLIER_ACCOUNT", result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.setViewName("manager2/financial/exAndReInfo");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/withDrawInfo/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView withDrawInfo(ModelAndView mav,
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		SupplierPayrecordQuery query = new SupplierPayrecordQuery();
		if(supplierId != 1) { 
			query.setSupplierId(supplierId);
			
			//获取渠道商的总余额
			BigDecimal balance=new BigDecimal(0);
			ResultDO result0 =suppliersService.getAccount(supplierId);
			SupplierAccountDO supplierAccountDO =(SupplierAccountDO) result0.getModel(ResultSupport.FIRST_MODEL_KEY);
			if(supplierAccountDO!=null){
				balance=supplierAccountDO.getwABalance();
			}
			mav.addObject("totals", balance);
		}
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		
		ResultDO result = suppliersService.listPayrecord(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/financial/withDrawInfo");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/smsInfo/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView smsInfo(ModelAndView mav, 
			HttpServletRequest request,
			@PathVariable(value="pageNo") Integer pageNo){
		
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		SmsRecordQuery query = new SmsRecordQuery();
		query.setSupplierId(supplierId);
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		System.out.println(supplierId);
		ResultDO result = smsService.recordList(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.addObject("SMS_COUNT",result.getModel(ResultSupport.THIRD_MODEL_KEY));
			mav.setViewName("manager2/financial/smsInfo");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/smsInfoA/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView smsInfoA(ModelAndView mav, 
			HttpServletRequest request,
			@PathVariable(value="pageNo") Integer pageNo){
		
		SmsRecordQuery query = new SmsRecordQuery();
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		ResultDO result = smsService.recordList(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.addObject("SMS_COUNT",result.getModel(ResultSupport.THIRD_MODEL_KEY));
			mav.setViewName("manager2/financial/smsInfoA");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/countAccount.html", method = RequestMethod.GET)
	public ModelAndView countAccount(ModelAndView mav, HttpServletRequest request) {
	
		ResultDO result = suppliersService.getCountAccount();
		if(result.isSuccess()) {
			mav.addObject("SUPPLIER_ACCOUNT", result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.setViewName("manager/financial/accountInfo");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/accountList/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView query(ModelAndView mav,
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		
		SupplierAccountQuery query = new SupplierAccountQuery();
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		
		String wSName=request.getParameter("name");
		if(wSName!=null && !("".equals(wSName))){
			query.setSupplierName(wSName);
		}
		
		ResultDO result = suppliersService.listAccount(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager/supplier/account");
		} else {
			mav.setViewName("error");
		}
		return mav;
		
	}
	
	@RequestMapping(value = "/smsList/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView smsList(ModelAndView mav, 
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		SmsRecordQuery query = new SmsRecordQuery();
		query.setSupplierId(supplierId);
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		System.out.println(supplierId);
		ResultDO result = smsService.recordList(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.addObject("SMS_COUNT",result.getModel(ResultSupport.THIRD_MODEL_KEY));
			mav.setViewName("manager/supplier/smsList");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/listPayrecord/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView listPayrecord(ModelAndView mav, 
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		SupplierPayrecordQuery query = new SupplierPayrecordQuery();
		if(supplierId != 1) { 
			query.setSupplierId(supplierId);
		}
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		
		ResultDO result = suppliersService.listPayrecord(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager/supplier/listPayrecord");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/listAccountRecord/{type}/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView listAccountRecord(ModelAndView mav, 
			@PathVariable(value="type") Integer type,
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		
		AccountRecordQuery query = new AccountRecordQuery();
		if(type != 0) { 
			query.setwType(type);
		}
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		
		ResultDO result = supplierAccountService.listAccountRecord(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager/financial/listRecord");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/listWithDrawInfo/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView listWithDrawInfos(ModelAndView mav, 
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		
		SupplierPayrecordQuery query = new SupplierPayrecordQuery();
		
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		query.setStatus(2);
		
		ResultDO result = suppliersService.listPayrecord(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager/financial/withDraw");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/listWithDrawInfoP/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView listWithDrawInfo(ModelAndView mav, 
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		
		SupplierPayrecordQuery query = new SupplierPayrecordQuery();
		
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		query.setStatus(1);
		
		ResultDO result = suppliersService.listPayrecord(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager/financial/watingWithDraw");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/heiInfo/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView listSArecord(ModelAndView mav, 
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		SupplierAccountRecordQuery query = new SupplierAccountRecordQuery();	
		ResultDO sresultDO=suppliersService.get(supplierId);
		if(sresultDO.isSuccess()) {
			SupplierDO supplierDO=(SupplierDO) sresultDO.getModel(ResultSupport.FIRST_MODEL_KEY);
			int num=0;
			if(supplierDO.getwSMoney()!=null){
				num=supplierDO.getwSMoney();
			}
			mav.addObject("suMoney",num);
		}else {
			mav.setViewName("error");
			return mav;
		}
		if(supplierId != 1) { 
			query.setwSupplierId(supplierId);
		}
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		
		ResultDO result = suppliersService.listSArecord(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/financial/heiInfo");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/heiInfoA/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView listSArecordA(ModelAndView mav, 
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request) {
		SupplierAccountRecordQuery query = new SupplierAccountRecordQuery();	
		
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		
		ResultDO result = suppliersService.listSArecord(query);
		
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/financial/heiInfoA");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/pay.html", method = RequestMethod.GET)
	public ModelAndView pay(ModelAndView mav, HttpServletRequest request) {
		mav.setViewName("manager/supplier/pay");
		return mav;
	}

	/*
	 * 跳转到申请提现
	 * 
	 */
	@RequestMapping(value = "/topayRecord.html", method = RequestMethod.GET)
	public ModelAndView topayRecord(ModelAndView mav, 
			HttpServletRequest request) {
		
		mav.setViewName("manager2/financial/withDrawInfoAdd");
		return mav;
	}
	
	
	/*
	 * 申请提现
	 * 
	 */
	@RequestMapping(value = "/payRecord.html", method = RequestMethod.POST)
	public ModelAndView payRecord(ModelAndView mav, 
			SupplierPayrecordDO supplierPayrecordDO,
			HttpServletRequest request) {
		
		supplierPayrecordDO.setCreateAuthor((String) request.getSession().getAttribute("MANAGER_NAME"));
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		supplierPayrecordDO.setModifiAuthor(supplierPayrecordDO.getCreateAuthor());
		supplierPayrecordDO.setwPSupperId(supplierId);
		
		ResultDO result = suppliersService.pay(supplierPayrecordDO);
		if(result.isSuccess()) {
			return new ModelAndView("redirect:/supplier/withDrawInfo/1.html");
		}else{
			mav.setViewName("error");
		}
		return mav;
	}
	
	/**
	 * 审核通过申请提现功能
	 * @param mav
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/payend/{id}.json", method = RequestMethod.GET)
	public ModelAndView payend(ModelAndView mav, 
			@PathVariable(value="id") Integer id,
			HttpServletRequest request) {
		
		JsonResultDO jsonResultDO = new JsonResultSupport();
		
		
		//修改记录状态
		ResultDO result = suppliersService.payStatus(id);
		//渠道商金额变化
		ResultDO alls = suppliersService.updateSupplierAccount(id);
		if(alls.isSuccess()) {
			if(result.isSuccess()) {
				jsonResultDO.setInfo("成功");
				jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
				mav.addObject("JSON_DATA", jsonResultDO);
				
			} else {
				jsonResultDO.setInfo("失败");
				jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
				mav.addObject("JSON_DATA", jsonResultDO);
			}
		}else {
			jsonResultDO.setInfo("失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	} 
	
}
